package cn.tedu.micro.demo.es;

import cn.tedu.micro.demo.es.po.StudentEsPO;
import cn.tedu.micro.demo.es.repository.StudentEsRepository;
import cn.tedu.micro.demo.es.utils.NickNameGenerator;
import cn.tedu.micro.demo.es.utils.ParseDateUtil;
import cn.tedu.micro.demo.es.utils.StudentDataGenerator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.unit.DataUnit;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@SpringBootTest
public class EsTestDemo01 {
    //es的基础读写工作
    @Autowired
    private StudentEsRepository studentEsRepository;
    //测试save
    @Test
    public void saveStudent(){
        StudentEsPO po=new StudentEsPO(
                UUID.randomUUID().toString(),
                "2504VN0001",
                NickNameGenerator.generate(),
                18,
                ParseDateUtil.parse("2019-01-01 00:00:00"),
                "中国北京市朝阳区望京镇葫芦村",null);
        studentEsRepository.save(po);
    }
    @Test
    public void saveStudents(){
        //1.使用工具 生成20个studentEsPO集合
        List<StudentEsPO> studentEsPOS = StudentDataGenerator.generateRandomStudents(49);
        //2.调用批量写入es
        studentEsRepository.saveAll(studentEsPOS);
    }
    @Test
    public void getStudentByID(){
        String id="54d2dab3-a714-4ace-8ed7-ee0e9917bc71";
        //Optional是一个装在数据的容器类型,当前es中没有其他用途
        StudentEsPO studentEsPO = studentEsRepository.findById(id).get();
        System.out.println(studentEsPO);
    }
    @Test
    public void getStudentsByIDs(){
        List<String> ids=List.of(
                "54d2dab3-a714-4ace-8ed7-ee0e9917bc71",
                "cf3db652-2761-4c12-9a2a-6795af2f5c69",
                "bb380b7d-5249-43b6-b06b-0dd5017f83c1");
        Iterable<StudentEsPO> allPos = studentEsRepository.findAllById(ids);
        //循环遍历这个迭代器
        for (StudentEsPO po : allPos) {
            System.out.println(po);
        }
    }
    @Test
    public void deleteStudentByID(){
        String id="54d2dab3-a714-4ace-8ed7-ee0e9917bc71";
        studentEsRepository.deleteById(id);
    }

    @Test
    public void deleteStudent(){
        StudentEsPO po=new StudentEsPO(
                "bb380b7d-5249-43b6-b06b-0dd5017f83c1",
                "2504VN810100",
                null,
                null,
                null,
                null,
                null);
        studentEsRepository.delete(po);
    }
}
